<?php
/**
 * Created by PhpStorm.
 * User: wx:wenyi2048
 * Date: 2022/3/29 0029
 * Time: 14:48
 */

namespace app\common\service;


use app\common\model\mk\Users;

class AddressService extends Service
{
    protected $tableName = 'sms_mk_address';

    /**
     * @return array api获取用户地址
     */
    public function datalist()
    {
        $this->db->where('sms_users_id','=',$this->param['sms_users_id']);
        if(@$this->param['def_id'])
            $this->db->orderRaw('id<>'.$this->param['def_id']);//默认地址放第一位
        $this->db->where('isdelete','=',0);
        return parent::datalist(); // TODO: Change the autogenerated stub
    }

    /**
     * @return int|string api编辑
     */
    public function edit()
    {
        $this->db->where('sms_users_id','=',$this->param['sms_users_id']);
        return parent::edit(); // TODO: Change the autogenerated stub
    }

    public function add()
    {
        $isDefault = @$this->param['is_default'];
        unset($this->param['is_default']);
        $addressId = parent::add(); // TODO: Change the autogenerated stub
        if(@$isDefault){
            $user = Users::find($this->param['sms_users_id']);
            $user->sms_address_id = $addressId;
            $user->save();
        }
        return $addressId;
    }

    /**
     * 获取用户指定地址
     */
    public function getUserAddress($id,$userId = null){
        $this->db->where('id','=',$id);
        if($userId) $this->db->where('sms_users_id','=',$userId);
        return $this->db->find();
    }

    //api delete
    public function delete(){
        $this->db->where('id','=',$this->param['id']);
        $this->db->where('sms_users_id','=',$this->param['sms_users_id']);
        return $this->db->update(['isdelete'=>1]);
    }

}